/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.daoImpl;

import br.com.deployment.dao.NotaryOfficeDAO;
import br.com.deployment.entities.Executor;
import br.com.deployment.entities.NotaryOffice;
import br.com.deployment.genericdao.GenericDAO;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
@LocalBean
public class NotaryOfficeDAOImpl extends GenericDAO<NotaryOffice> implements NotaryOfficeDAO {
    
    public NotaryOfficeDAOImpl() {
        super(NotaryOffice.class);
    }
    
    @Override
    public List<NotaryOffice> findNotaryOffice(NotaryOffice notary) {
        List<NotaryOffice> lista = new ArrayList<NotaryOffice>();
        
        String sql = "SELECT n FROM NotaryOffice n WHERE ";
        
        if ((notary.getNome() != "") && (notary.getNome() != null)) {
            sql += " lower(n.nome) like lower(:nome) and  ";
        }
        
        if ((notary.getTitular() != "") && (notary.getTitular() != null)) {
            sql += " lower(n.titular) like lower(:titular) and  ";
        }
        
        sql += " 1=1 ";
        System.err.println(sql);
        Query query = getEntityManager().createQuery(sql);
        
        if ((notary.getNome() != "") && (notary.getNome() != null)) {
            query.setParameter("nome", "%" + notary.getNome() + "%");
        }
        if ((notary.getTitular() != "") && (notary.getTitular() != null)) {
            query.setParameter("titular", "%" + notary.getTitular() + "%");
        }
        
        lista = query.getResultList();
        System.err.println(sql);
        return lista;
    }

    @Override
    public List<NotaryOffice> autoCompleteNotaryOffice(NotaryOffice notary) {
        List<NotaryOffice> lista = new ArrayList<NotaryOffice>();
        String sql = "SELECT n FROM NotaryOffice n  ";
        
        if ((notary.getNome() != "") && (notary.getNome() != null)) {
            sql += " WHERE lower(n.nome) like lower(:nome) ";
        } 
        Query query = getEntityManager().createQuery(sql);
        
        if ((notary.getNome() != "") && (notary.getNome() != null)) {
            query.setParameter("nome", "%" + notary.getNome() + "%");
        } 
       
        lista = query.getResultList();
        System.err.println(sql);
        return lista;
    }
}
